package org.ofbiz.wvtools.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;

public class ExcelPoiUtils {
    public static final String module = ExcelPoiUtils.class.getName();

    public static String getDataDictionaryDescription(Delegator delegator, String dictionaryTypeId,
	    String dictionaryCode) throws GenericEntityException {
	String result = "";
	if (UtilValidate.isEmpty(dictionaryCode)) {
	    return result;
	}
	List<GenericValue> values = delegator.findByAnd("DataDictionary",
		UtilMisc.toMap("dictionaryTypeId", dictionaryTypeId, "dictionaryCode", dictionaryCode), null, true);
	GenericValue dataDictionary = EntityUtil.getFirst(values);
	if (UtilValidate.isNotEmpty(dataDictionary)) {
	    result = dataDictionary.getString("description");
	}
	return result;
    }

    public static String getDataDictionaryCode(Delegator delegator, String dictionaryTypeId, String description)
	    throws GenericEntityException {
	String result = "";
	if (UtilValidate.isEmpty(description)) {
	    return result;
	}
	List<GenericValue> values = delegator.findByAnd("DataDictionary",
		UtilMisc.toMap("dictionaryTypeId", dictionaryTypeId, "description", description), null, true);
	GenericValue dataDictionary = EntityUtil.getFirst(values);
	if (UtilValidate.isNotEmpty(dataDictionary)) {
	    result = dataDictionary.getString("dictionaryCode");
	}
	return result;
    }

    public static String getCellString(Map<String, Object> row, int col) {
	if (UtilValidate.isEmpty(row.get("" + col))) {
	    return "";
	}
	return String.valueOf(row.get("" + col)).trim();
    }

    public static BigDecimal getCellBigDecimal(Map<String, Object> row, int col) {
	if (UtilValidate.isEmpty(row.get("" + col))) {
	    return null;
	}
	return new BigDecimal(String.valueOf(row.get("" + col)).trim());
    }

    public static Long getCellLong(Map<String, Object> row, int col) {
	if (UtilValidate.isEmpty(row.get("" + col))) {
	    return null;
	}
	return Long.valueOf(String.valueOf(row.get("" + col)).trim());
    }

    public static Timestamp getCellTimestamp(Map<String, Object> row, int col) {
	if (UtilValidate.isEmpty(row.get("" + col))) {
	    return null;
	}
	Timestamp tt = null;
	if (UtilValidate.isFloat(String.valueOf(row.get("" + col)))) {
	    double dateNum = Double.valueOf(String.valueOf(row.get("" + col)));
	    int num = (int) Math.floor(dateNum); // 天数
	    double secondNum = (dateNum * 100 - num * 100) * 24 / 100 * 3600; // 秒
	    Calendar c = new GregorianCalendar(1900, 0, -1);
	    java.util.Date d = c.getTime();
	    java.util.Date _d = DateUtils.addDays(d, num);
	    _d = DateUtils.addSeconds(_d, (int) secondNum);
	    tt = new Timestamp(_d.getTime());
	    return tt;
	} else {
	    tt = Timestamp.valueOf(String.valueOf(row.get("" + col)).trim());
	    return tt;
	}
    }

    public static Timestamp getCellTimestamp2(Map<String, Object> row, int col) {
	if (UtilValidate.isEmpty(row.get("" + col))) {
	    return null;
	}
	Timestamp tt = null;
	if (UtilValidate.isFloat(String.valueOf(row.get("" + col)))) {
	    Date tDate = DoubleToDate(Double.parseDouble(String.valueOf(row.get("" + col))));
	    Timestamp createTime = new Timestamp(tDate.getTime());
	    return createTime;
	} else {
	    Debug.log("xxx==="+row.get("" + col));
	    String s = String.valueOf(row.get("" + col)).trim();
	    if(!s.contains(":")) {
		s = s + " 00:00:00";
		s = s.replaceAll("/", "-");
	    }
	    tt = Timestamp.valueOf(s);
	    return tt;
	}
    }

    public static Date DoubleToDate(Double dVal) {
	Date tDate = new Date();
	long localOffset = tDate.getTimezoneOffset() * 60000; // 系统时区偏移 1900/1/1 到 1970/1/1 的 25569 天
	tDate.setTime((long) ((dVal - 25569) * 24 * 3600 * 1000 + localOffset));

	return tDate;
    }

    public static Long getNowMonth(Timestamp date) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	String month = new SimpleDateFormat("MM").format(cal.getTime());
	return Long.valueOf(month);
    }

    public static Long getNowYear(Timestamp date) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	String year = new SimpleDateFormat("yyyy").format(cal.getTime());
	return Long.valueOf(year);
    }

    public static String getCurrentYear() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
	Date date = new Date();
	return sdf.format(date);
    }
}
